define(function (require, exports, module) {

	var $ = require('$');
		require('bootstrap-notify');
		require('form-ajax');

	if ( typeof  $.notifyDefaults == 'function' ) {
		$.notifyDefaults({
			type : 'info',
			allow_dismiss : true,//关闭按钮
			placement : {//位置
				from : "top",
				align : "center"
			},
			delay : 500,
			timer : 800,//动画时间
			animate : {//动画效果
				enter : 'animated fadeInDown',
				exit : 'animated fadeOutUp'
			}
		})
	}

	//普通表单提交
	exports.ajaxForm = function(ajaxFrom){
		ajaxFrom.ajaxForm ({
			dataType:'json',
			success:function(data){
				if(data.status == 1){
					$.notify(data.info, {type : 'success'});
					if(data.url.length>0){
						setTimeout(function(){
							window.location.href = data.url;
						},1000);
					}
					return true;
				}
				$.notify(data.info, {type : 'danger'});
			}
		});
	};

	//模态框表单提交
	exports.ajaxModalSubmit = function(ajaxFrom){
		var modal = ajaxFrom.parents('.modal');
		var alertDanger  = $('.alert-danger');
		ajaxFrom.ajaxForm ({
			dataType:'json',
			success:function(data){
				if(data.status == 1){
					modal.modal('hide');
					$.notify(data.info, {type : 'success'});
					setTimeout(function(){
						window.location.reload();
					},1000);
					return true;
				}
				if(data.info){
					alertDanger.show().find('.alert-text').text(data.info);
				}
				$('[type=submit]').attr('disabled', false);
			},
			error:function(data, textStatus, errorThrown){
				alertDanger.find('ul').empty();
				var error = [];
				$.each(data.responseJSON,function(n,val){
					error = error.concat(val);
				});

				for (var i = 0; i < error.length; i++) {
					alertDanger.show();
					alertDanger.find('ul').append('<li>'+error[i]+'</li>')
				}
				$('[type=submit]').attr('disabled', false);
			}
		});

	};

	//a标签ajax方式点击
	exports.ajaxClick = function(element){
		element.click(function(event){
			event.preventDefault();
			var _this = $(this);
			if(confirm('是否要执行此操作？')){
				$.ajax({
					url:_this.attr('href'),
					type:'get',
					dataType:'json',
					success:function(data){
						if(data.status == 1){
							$.notify(data.info, {type : 'success'});
							setTimeout(function(){
								window.location.href = data.url;
							},1000);
							return true;
						}
						$.notify(data.info, {type : 'danger'});
					},
					error:function(data){
						$.notify('操作失败', {type : 'danger'});
					}
				})
			}
		})
	};


});